package cn.youqu.happy.module.youqu.controller.admin.arrangeEdit;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.arrangeEdit.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.arrangeEdit.ArrangeEditDO;
import cn.youqu.happy.module.youqu.convert.arrangeEdit.ArrangeEditConvert;
import cn.youqu.happy.module.youqu.service.arrangeEdit.ArrangeEditService;

@Api(tags = "管理后台 - 计划申请修改")
@RestController
@RequestMapping("/youqu/arrange-edit")
@Validated
public class ArrangeEditController {

    @Resource
    private ArrangeEditService arrangeEditService;

    @PostMapping("/create")
    @ApiOperation("创建计划申请修改")
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:create')")
    public CommonResult<Integer> createArrangeEdit(@Valid @RequestBody ArrangeEditCreateReqVO createReqVO) {
        return success(arrangeEditService.createArrangeEdit(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新计划申请修改")
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:update')")
    public CommonResult<Boolean> updateArrangeEdit(@Valid @RequestBody ArrangeEditUpdateReqVO updateReqVO) {
        arrangeEditService.updateArrangeEdit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除计划申请修改")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:delete')")
    public CommonResult<Boolean> deleteArrangeEdit(@RequestParam("id") Integer id) {
        arrangeEditService.deleteArrangeEdit(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得计划申请修改")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:query')")
    public CommonResult<ArrangeEditRespVO> getArrangeEdit(@RequestParam("id") Integer id) {
        ArrangeEditDO arrangeEdit = arrangeEditService.getArrangeEdit(id);
        return success(ArrangeEditConvert.INSTANCE.convert(arrangeEdit));
    }

    @GetMapping("/list")
    @ApiOperation("获得计划申请修改列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:query')")
    public CommonResult<List<ArrangeEditRespVO>> getArrangeEditList(@RequestParam("ids") Collection<Integer> ids) {
        List<ArrangeEditDO> list = arrangeEditService.getArrangeEditList(ids);
        return success(ArrangeEditConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得计划申请修改分页")
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:query')")
    public CommonResult<PageResult<ArrangeEditRespVO>> getArrangeEditPage(@Valid ArrangeEditPageReqVO pageVO) {
        PageResult<ArrangeEditDO> pageResult = arrangeEditService.getArrangeEditPage(pageVO);
        return success(ArrangeEditConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出计划申请修改 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:arrange-edit:export')")
    @OperateLog(type = EXPORT)
    public void exportArrangeEditExcel(@Valid ArrangeEditExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ArrangeEditDO> list = arrangeEditService.getArrangeEditList(exportReqVO);
        // 导出 Excel
        List<ArrangeEditExcelVO> datas = ArrangeEditConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "计划申请修改.xls", "数据", ArrangeEditExcelVO.class, datas);
    }

}
